// Copyright 2012. InMobi. All Rights reserved

package com.inmobi.sharedlock;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
 *
 */
public class ThreadSafeCalculator5 implements AverageCalculator {

  private AtomicLong sum = new AtomicLong(0);

  private AtomicInteger count = new AtomicInteger(0);

  @Override
  public void addValue(long value) {
    count.incrementAndGet();
    sum.addAndGet(value);
  }

  @Override
  public double getAverage() {
    long countValue = count.get();
    double sumValue = (double) sum.get();
    return countValue != 0 ? sumValue / countValue : 0.0;
  }

}
